package com.lihuan.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.lihuan.beans.*;
import com.lihuan.common.util.Result;
import com.lihuan.common.util.ResultData;
import com.lihuan.common.util.ResultPage;
import com.lihuan.entity.Message;
import com.lihuan.service.MessageService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 留言操作控制器
 *
 * @author lihuan
 * <p>
 * 2020年5月14日
 */
@RestController
@RequestMapping(consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = MediaType.APPLICATION_JSON_VALUE)
public class MessageController {

    @Resource
    MessageService messageService;

    /**
     * 查询留言列表
     *
     * @param param 查询留言列表参数
     * @return rest结果
     */
    @PostMapping("/message/list")
    public ResultPage<MessageListItem> listMessage(@Validated @RequestBody MessageList param) {
        PageInfo<MessageListItem> bean = messageService.listMessage(param);
        return new ResultPage<MessageListItem>(bean);
    }

    /**
     * 查询留言列表
     *
     * @param param 查询留言列表参数
     * @return rest结果
     */
    @PostMapping("/message/list/person")
    public ResultPage<MessageListItem> listPersonMessage(@Validated @RequestBody MessageList param) {
        PageInfo<MessageListItem> bean = messageService.listPersonMessage(param);
        return new ResultPage<MessageListItem>(bean);
    }

    /**
     * 添加留言
     *
     * @param param 添加留言参数
     * @return rest结果
     */
    @PostMapping(value = "/message/add")
    public Result addMessage(@Validated @RequestBody MessageAdd param) {
        messageService.addMessage(param);
        return Result.SUCCESS;
    }

    /**
     * 修改留言
     *
     * @param param 修改留言参数
     * @return rest结果
     */
    @PostMapping(value = "/message/edit")
    public Result editMessage(@Validated @RequestBody MessageEdit param) {
        messageService.editMessage(param);
        return Result.SUCCESS;
    }

    /**
     * 删除留言
     *
     * @param param 删除留言参数
     * @return rest结果
     */
    @PostMapping(value = "/message/remove")
    public Result removeMessage(@Validated @RequestBody MessageId param) {
        messageService.removeMessage(param);
        return Result.SUCCESS;
    }

    /**
     * 详细留言
     *
     * @param param 详细留言参数
     * @return rest结果
     */
    @PostMapping(value = "/message/info")
    public ResultData<MessageInfo> infoMessage(@Validated @RequestBody MessageId param) {
        MessageInfo data = messageService.infoMessage(param);
        return new ResultData<MessageInfo>(data);
    }

    /**
     * list转tree
     *
     * @param
     * @return rest结果
     */
    @PostMapping(value = "/list/to/tree")
    public JSONArray listToTree() {
        return messageService.listToTree();
       // return new ResultData<MessageInfo>(data);
    }
}
